package com.zyl.service.Impl;

import com.zyl.Mapper.CommentRecordMapper;
import com.zyl.Mapper.UserMapper;
import com.zyl.pojo.CommentRecord;
import com.zyl.pojo.User;
import com.zyl.service.CommentRecordService;
import com.zyl.vo.AdminRecordVo;
import com.zyl.vo.CommentsVo;
import com.zyl.vo.DataVo;
import com.zyl.vo.ReplyVo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Zyl
 * @Param:
 * @return:
 * @Description:
 */
@Service("CommentRecordService")

public class CommentRecordServiceImpl implements CommentRecordService {
    @Autowired
    CommentRecordMapper commentRecordMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public List<AdminRecordVo> findAllComment() {
        List<AdminRecordVo> Comment = commentRecordMapper.findAllComment().stream().map(e -> new AdminRecordVo(
                e.getId(),
                e.getCommentDate(),
                e.getArticleId(),
                e.getCommentContent(),
                e.getAnswererId(),
                e.getLikes()
        )).collect(Collectors.toList());
        return Comment;
    }

    @Override
    public void deleteCommentById(Integer id) {
        commentRecordMapper.deleteCommentById(id);
    }

    @Override
    public DataVo findCommentByArticleId(Long articleId) {
        List<CommentRecord> commentByArticleId = commentRecordMapper.findCommentByArticleId(articleId);
        DataVo dataVo = new DataVo();
        List<CommentsVo> commentsVo = new ArrayList<>();
        List<ReplyVo> replyVo= new ArrayList<>();
        int i=0;
        for (CommentRecord commentRecord : commentByArticleId) {
            if (commentRecord.getPId() == -1) {
                Integer answererId = commentRecord.getAnswererId();
                User user = userMapper.queryUserById(answererId);
                CommentsVo commentsVo1 = new CommentsVo();
                commentsVo1.setName(user.getTrueName());
                commentsVo1.setId(user.getId());
                commentsVo1.setHeadImg(user.getAvatarImgUrl());
                commentsVo1.setTime(commentRecord.getCommentDate());
                commentsVo1.setComment(commentRecord.getCommentContent());
                commentsVo1.setLike(commentRecord.getLikes());
                for (CommentRecord record1 : commentByArticleId) {
                    if (i == record1.getPId()) {
                            ReplyVo replyVo1 = new ReplyVo();
                                User user1 = userMapper.queryUserById(record1.getAnswererId());
                                replyVo1.setFrom(user1.getTrueName());
                                replyVo1.setFromId(user1.getId());
                            replyVo1.setFromHeadImg(user1.getAvatarImgUrl());
                                User user2 = userMapper.queryUserById(record1.getRespondentId());
                                replyVo1.setTo(user2.getTrueName());
                                replyVo1.setToId(user2.getId());
                                replyVo1.setComment(record1.getCommentContent());
                                replyVo1.setTime(record1.getCommentDate());
                        //Mapper中的commentRecordMapper.ResponseCount 的参数有问题 待解决
//                        Integer respondentId = record1.getRespondentId();
//                        Long aLong = Long.valueOf(respondentId);
//                        Integer integer = commentRecordMapper.ResponseCount(aLong, articleId);
//                        commentsVo1.setCommentNum(integer);
                                replyVo1.setLike(record1.getLikes());
                                replyVo.add(replyVo1);
                                commentsVo1.setReply(replyVo);

                    }
                }

                i++;
                commentsVo.add(commentsVo1);


            }
            replyVo=new ArrayList<>();
            dataVo.setComments(commentsVo);
        }
        return dataVo;

    }

    @Override
    public void addComment(CommentRecord commentRecord) {
        commentRecordMapper.addComment(commentRecord);
    }

    @Override
    public void addReply(CommentRecord commentRecord) {
        commentRecordMapper.addReply(commentRecord);
    }

    @Override
    public Integer commentCount() {
        return commentRecordMapper.commentCount();
    }
}
